package org.ccay.async.config;

import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
import org.springframework.beans.factory.xml.ParserContext;
import org.w3c.dom.Element;

import org.ccay.core.util.StringUtil;

/**
 * MessageConfig的Spring Bean解析器。
 * 	
 * 
 * @since Dec 12, 2011
 */
public abstract class MessageConfigParser extends AbstractSingleBeanDefinitionParser {
	
	private static final String MESSAGE_TYPE_ATTRIBUTE = "messageType";
	
	private static final String CONTEXT_AWARE_ATTRIBUTE = "contextAware";

	/* (non-Javadoc)
	 * @see org.springframework.beans.factory.xml.AbstractBeanDefinitionParser#shouldGenerateId()
	 */
	@Override
	protected boolean shouldGenerateId() {
		return true;
	}

	/* (non-Javadoc)
	 * @see org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser#doParse(org.w3c.dom.Element, org.springframework.beans.factory.xml.ParserContext, org.springframework.beans.factory.support.BeanDefinitionBuilder)
	 */
	@Override
	protected void doParse(Element element, ParserContext ctx, BeanDefinitionBuilder bean) {
		// 消息的类型
		String type = element.getAttribute(MESSAGE_TYPE_ATTRIBUTE);
		if(!StringUtil.isNullOrEmpty(type)) {
			bean.addPropertyValue(MESSAGE_TYPE_ATTRIBUTE, type);
		}
		
		// 上下文感知
		String contextAware = element.getAttribute(CONTEXT_AWARE_ATTRIBUTE);
		if(!StringUtil.isNullOrEmpty(contextAware)) {
			bean.addPropertyValue(CONTEXT_AWARE_ATTRIBUTE, contextAware);
		}
	}

}
